<?php

namespace Home\Controller;

use Think\Controller;

class TaskController extends Controller{
    
    public function task_list(){
        $user_id = session('user.id');
        // 获取当前用户的积分
        $point = M('user')->where(array('id'=>$user_id))->getField('point');
        
        // 获取任务栏目
        $navs = M('task_nav')->field('id')->select();
        
        $tasks = array();
        foreach ($navs as $k=>$v){
            // 获取当前栏目下的所有任务
            $task = M('task')->where(array('nav_id'=>$v['id']))->select();
            
            foreach ($task as $t){
                $user_task = M('user_task')->where(array('user_id'=>$user_id,'task_id'=>$t['id']))->find();
                if (empty($user_task)){
                    $user_task= array(
                        'user_id' =>$user_id,
                        'task_id' => $t['id'],
                        'status' => 0,
                    );
                    M('user_task')->add($user_task);
                    $task = $t;
                    break;
                }else {
                    if($user_task['status'] != 1){
                        $task = $t;
                        break;
                    }else{
                        $user_task = array();
                        $task = array();
                    }
                }
            }
            // 获取任务进度
            $finish_count = M('finish_count')->where(array('user_id'=>$user_id,'task_nav_id'=>$v['id']))->find();
            
            if(empty($finish_count)){
                $finish_count = array(
                    'user_id' =>$user_id,
                    'task_nav_id' => $v['id'],
                    'finish' => 0,
                );
                M('finish_count')->add($finish_count);
            }
            $user_task['finish'] = $finish_count['finish'];
            
            // 判断任务是否完成
            if ($user_task['finish'] >= $task['count']){
                $user_task['status'] = 1;
            }
            
            $tasks[$k]['task'] = $task;
            $tasks[$k]['user_task'] = $user_task;
        }
        
        // 获取所有已完成的任务
        $comTasks = M('user_task')->alias('u')->join('blog_task t')->where('t.id = u.task_id and u.status = 1')->field('t.task_name,t.reward')->select();
        
        $this->assign('comTasks',$comTasks);
        $this->assign('tasks',$tasks);
        $this->assign('point',$point);
        
        $this->display('User/taskList');
    }
    
    // 领取奖励
    public function reward(){
        $user_id = session('user.id');
        $task_id = I('post.task_id');
        $user_task_id = I('post.user_task_id');
        
        // 修改任务的状态
        $res = M('user_task')->where(array('id'=>$user_task_id))->setField('status',1);
        
        if($res !== false){
            // 获取任务奖励积分
            $reward = M('task')->where(array('id'=>$task_id))->getField('reward');
            
            // 修改用户积分
            $res = M('user')->where(array('id'=>$user_id))->setInc('point',$reward);
            if($res !== false){
                $count = session('user.point') + $reward;
                session('user.point',$count);
                echo 1;die;
            }
        }
        echo 0;die;
    }
 
}